<?php

namespace app\controller;

use App\model\Email;
use App\model\EmailCate;
use App\model\Inform;
use support\Common;
use support\Request;
use support\Response;

class IndexController
{

    public function getEmail(Request $request)
    {
        // $params = $request->post();
        $params = $request->postData;
        Common::writeLog([$params, '测试啊'], __FILE__, __FUNCTION__, __LINE__);
        $cate = $params['cate'];

        $email = new Email();
        $data = $email->getEmailBycate($cate);

        return json(['code' => 0, 'msg' => 'success', 'data' => $data]);
    }


    public function getCate(Request $request)
    {
        // $params = $request->post();

        $params = $request->postData;
        $data = EmailCate::query()->get()->toArray();

        //        var_dump($data);
        $list = [];
        foreach ($data as $datum) {
            $list[] = [
                'id' => $datum['id'],
                'name' => $datum['name']
            ];
        }


        return json(['code' => 0, 'msg' => 'success', 'data' => $list]);
    }

    /***
     * 公告类型
     * @param Request $request
     * @return Response
     */
    public function informTypes(Request $request): Response
    {
        $inform_types = Common::getDict('inform');
        $temp = [];
        foreach ($inform_types as $type => $name) {
            $temp[] = [
                'type' => $type,
                'name' => $name,
            ];
        }
        return json(['code' => 0, 'msg' => 'ok', 'data' => ['types' => $temp, 'default' => 1]]);
    }

    /***
     * 公告类型
     * @param Request $request
     * @return Response
     */
    public function inform(Request $request): Response
    {
        $params = $request->postData;
        $type = $params['type'] ?? 1;
        $inform = Inform::query()->where('type', $type)->first();
        $inform = $inform ? $inform->toArray() : [];
        var_dump($type);
        return json(['code' => 0, 'msg' => 'ok', 'data' => ['content' => $inform['content']]]);
    }


}
